import 'package:check_app/dao/collectList_dao.dart';
import 'package:check_app/model/collectList.dart';
import 'package:flutter/material.dart';
import 'package:check_app/widgets/Picture.dart';
import 'package:fluttertoast/fluttertoast.dart';

class CollectDetailPage extends StatefulWidget {
  final int id;
  const CollectDetailPage({
    Key key,
    this.id,
  }) : super(key: key);
  @override
  State<StatefulWidget> createState() => new _CollectDetailPageState();
}

class _CollectDetailPageState extends State<CollectDetailPage> {
  bool _loading = false;
  int currentIndex = 0;
  int id;
  int taskId;
  String taskCode;
  String customerName;
  String subName;
  String code;
  String checkTime;
  String verdictType;
  String verdictTypeText;
  List specList = [];
  List<String> picUrlList = [];
  List<Map> infoList = [];
  List collectList = [];

  @override
  void initState() {
    super.initState();
    getData();
  }

  Future<Null> getData() async {
    setState(() {
      _loading = true;
    });
    try {
      var result = await CollectListDao.fetch(
          '/subject/collect/' + widget.id.toString() + '/list', {
        'collectId': widget.id.toString(),
      });
      setState(() {
        _loading = false;
      });
      if (result.success) {
        setState(() {
          collectList = result.data;
          changeData(collectList[currentIndex]);
        });
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  changeData(item) {
    setState(() {
      id = item.id;
      taskId = item.taskId;
      taskCode = item.taskCode;
      customerName = item.customerName;
      subName = item.subName;
      code = item.code;
      checkTime = item.checkTime;
      verdictType = item.verdictType;
      verdictTypeText = item.verdictTypeText;
      specList = item.specList;
      if (specList.length == 0 ||
          specList[specList.length - 1].name != '检查结论') {
        specList.add(new SpecInfo(null, '检查结论', '', '', '', verdictTypeText));
      }
      picUrlList = item.picUrlList;
      infoList = [
        {
          'title': '所属任务',
          'key': taskCode,
        },
        {
          'title': '所属客户',
          'key': customerName,
        },
        {
          'title': '待检物编码',
          'key': code,
        },
        {
          'title': '待检物名称',
          'key': subName,
        },
      ];
    });
  }

  setAppTitle() {
    if (collectList.length > 1) {
      return (subName ?? '') +
          '(' +
          (currentIndex + 1).toString() +
          '/' +
          collectList.length.toString() +
          ')';
    } else {
      return subName ?? '';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(setAppTitle()),
      ),
      body: Stack(
        children: <Widget>[
          SingleChildScrollView(
            child: Container(
              padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
              child: Column(
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                    decoration: BoxDecoration(
                      border: Border(
                        bottom: BorderSide(
                          width: 1,
                          color: Color.fromRGBO(229, 229, 229, 1),
                        ),
                      ),
                    ),
                    child: Column(
                      children: getInfoList(infoList),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                    decoration: BoxDecoration(
                      border: Border(
                        bottom: BorderSide(
                          width: 1,
                          color: Color.fromRGBO(229, 229, 229, 1),
                        ),
                      ),
                    ),
                    child: Column(
                      children: getSpecList(specList),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.fromLTRB(0, 0, 0, 16),
                    margin: EdgeInsets.only(top: 8),
                    child: Column(
                      children: <Widget>[
                        Container(
                          alignment: Alignment.centerLeft,
                          margin: EdgeInsets.only(top: 12, bottom: 12),
                          child: Text(
                            '图片',
                            style: TextStyle(
                                fontSize: 14,
                                height: 20 / 14,
                                color: Color.fromRGBO(150, 151, 154, 1)),
                          ),
                        ),
                        Picture(
                          count: 4,
                          uploadButton: false,
                          picList: picUrlList,
                        ),
                      ],
                    ),
                  ),
                  getButton(),
                ],
              ),
            ),
          ),
          // Positioned(
          //   left: 0,
          //   right: 0,
          //   bottom: 20,
          //   child: getButton(),
          // ),
        ],
      ),
    );
  }

  getButton() {
    if (collectList.length == 1) {
      return SizedBox();
    } else {
      if (currentIndex == 0) {
        return Container(
          padding: EdgeInsets.fromLTRB(0, 16, 0, 16),
          width: double.infinity,
          child: MaterialButton(
            onPressed: () {
              currentIndex = currentIndex + 1;
              changeData(collectList[currentIndex]);
            },
            color: Colors.blue,
            textColor: Colors.white,
            height: 44,
            child: Text(
              '下一条',
              style: TextStyle(
                fontSize: 14,
                height: 24 / 17,
              ),
            ),
          ),
        );
      } else if (currentIndex + 1 < collectList.length) {
        return Container(
          padding: EdgeInsets.fromLTRB(0, 16, 0, 16),
          width: double.infinity,
          child: Row(
            children: <Widget>[
              MaterialButton(
                onPressed: () {
                  currentIndex = currentIndex - 1;
                  changeData(collectList[currentIndex]);
                },
                color: Color.fromRGBO(248, 248, 249, 1),
                textColor: Color.fromRGBO(89, 131, 252, 1),
                height: 44,
                minWidth: 160,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(2)),
                  side: BorderSide(
                    width: 1,
                    color: Color.fromRGBO(89, 131, 252, 1),
                    style: BorderStyle.solid,
                  ),
                ),
                child: Text(
                  '上一条',
                  style: TextStyle(
                    fontSize: 14,
                    height: 24 / 17,
                  ),
                ),
              ),
              Expanded(
                child: Container(
                  alignment: Alignment.centerRight,
                  child: MaterialButton(
                    onPressed: () {
                      currentIndex = currentIndex + 1;
                      changeData(collectList[currentIndex]);
                    },
                    color: Colors.blue,
                    textColor: Colors.white,
                    height: 44,
                    minWidth: 160,
                    child: Text(
                      '下一条',
                      style: TextStyle(
                        fontSize: 14,
                        height: 24 / 17,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      } else if (currentIndex + 1 == collectList.length) {
        return Container(
          padding: EdgeInsets.fromLTRB(0, 16, 0, 16),
          width: double.infinity,
          child: MaterialButton(
            onPressed: () {
              currentIndex = currentIndex - 1;
              changeData(collectList[currentIndex]);
            },
            color: Color.fromRGBO(248, 248, 249, 1),
            textColor: Color.fromRGBO(89, 131, 252, 1),
            height: 44,
            minWidth: 160,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(2)),
              side: BorderSide(
                width: 1,
                color: Color.fromRGBO(89, 131, 252, 1),
                style: BorderStyle.solid,
              ),
            ),
            child: Text(
              '上一条',
              style: TextStyle(
                fontSize: 14,
                height: 24 / 17,
              ),
            ),
          ),
        );
      }
    }
  }

  // 获取菜单项widget列表
  List<Widget> getInfoList(List info) {
    return info.map<Widget>((item) => createInfoItem(item)).toList();
  }

  // 生成单个菜单项
  Widget createInfoItem(item) {
    return Container(
      height: 40,
      child: Row(
        children: <Widget>[
          Container(
            alignment: Alignment.centerLeft,
            child: Text(
              item['title'],
              style: TextStyle(
                fontSize: 14,
                height: 20 / 14,
                color: Color.fromRGBO(150, 151, 154, 1),
              ),
            ),
          ),
          Expanded(
            child: Container(
              alignment: Alignment.centerRight,
              child: Text(
                item['key'] ?? '--',
                style: TextStyle(
                  fontSize: 14,
                  height: 20 / 14,
                  color: Color.fromRGBO(43, 44, 46, 1),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 获取规格widget列表
  List<Widget> getSpecList(List info) {
    return info.map<Widget>((item) => createSpecItem(item)).toList();
  }

  // 生成单个规格项
  Widget createSpecItem(item) {
    return Container(
      height: 40,
      child: Row(
        children: <Widget>[
          Container(
            alignment: Alignment.centerLeft,
            child: Text(
              item.name ?? '',
              style: TextStyle(
                fontSize: 14,
                height: 20 / 14,
                color: Color.fromRGBO(150, 151, 154, 1),
              ),
            ),
          ),
          Expanded(
            child: Container(
              alignment: Alignment.centerRight,
              child: Text(
                item.value ?? '--',
                style: TextStyle(
                  fontSize: 14,
                  height: 20 / 14,
                  color: Color.fromRGBO(43, 44, 46, 1),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
